// Visualization.java

import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class Visualization extends JFrame implements ActionListener {
    public static final int MIN_SIZE = 3;
    public static final int MAX_SIZE = 13;
    public static final String[] TYPES = {"行主序", "列主序", "对角序", "反对角序", "顺时针序", "逆时针序"};
    private int whichType = 0;
    private SquareTraversal squareTraversal = null;

    JRadioButton radioRowMajor = new JRadioButton("行主序");
    JRadioButton radioColMajor = new JRadioButton("列主序");
    JRadioButton radioDiagonal = new JRadioButton("对角序");
    JRadioButton radioAntiDiagonal = new JRadioButton("反对角序");
    JRadioButton radioClockwise = new JRadioButton("顺时针序");
    JRadioButton radioAntiClockwise = new JRadioButton("逆时针序");
    JComboBox boxSize = new JComboBox();
    JTextField fieldType = new JTextField();
    JTextField fieldSize = new JTextField();
    JButton buttonNew = new JButton("新遍历");
    JButton buttonNext = new JButton("下一步");

    JPanel panelShow = new JPanel();

    public Visualization() {
        // 在这里添加代码：
        //初始化可视化界面，创建并配置组件

        // 设置窗口标题
        setTitle("实验二 方阵遍历可视化  from 220501208 计算机23-1 邓旭昌");
        // 设置关闭操作
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 设置窗口大小
        setSize(1200, 800);
        // 设置布局为 BorderLayout
        setLayout(new BorderLayout());

        // 初始化大小选择框： 3->13
        for (int i = MIN_SIZE; i <= MAX_SIZE; i++) {
            boxSize.addItem(i);
        }

        // 创建按钮组
        ButtonGroup group = new ButtonGroup();
        group.add(radioRowMajor);
        group.add(radioColMajor);
        group.add(radioDiagonal);
        group.add(radioAntiDiagonal);
        group.add(radioClockwise);
        group.add(radioAntiClockwise);

        // 创建控制面板
        JPanel panelControl = new JPanel();
        panelControl.add(radioRowMajor);
        panelControl.add(radioColMajor);
        panelControl.add(radioDiagonal);
        panelControl.add(radioAntiDiagonal);
        panelControl.add(radioClockwise);
        panelControl.add(radioAntiClockwise);
        panelControl.add(new JLabel("设置方阵大小(边长): "));
        panelControl.add(boxSize);
        panelControl.add(new JLabel("当前遍历类型: "));
        panelControl.add(fieldType);
        fieldType.setEditable(false);//不接受用户输入
        panelControl.add(new JLabel("当前方阵大小: "));
        panelControl.add(fieldSize);
        fieldSize.setEditable(false);//不接受用户输入
        panelControl.add(buttonNew);
        panelControl.add(buttonNext);

        // 添加事件监听器
        radioRowMajor.addActionListener(this);
        radioColMajor.addActionListener(this);
        radioDiagonal.addActionListener(this);
        radioAntiDiagonal.addActionListener(this);
        radioClockwise.addActionListener(this);
        radioAntiClockwise.addActionListener(this);
        buttonNew.addActionListener(this);
        buttonNext.addActionListener(this);

        // 将控制面板添加到窗口北部
        add(panelControl, BorderLayout.NORTH);
        // 将显示面板添加到窗口中部
        add(panelShow, BorderLayout.CENTER);

        // 显示窗口
        setVisible(true);
    }
    public void actionPerformed(ActionEvent e) {
        // 在这里添加代码：
        //开始遍历
        if (e.getSource() == buttonNew) {
            int size = (int) boxSize.getSelectedItem();
            if (radioRowMajor.isSelected())
                squareTraversal=new RowMajor(size);
            else if (radioColMajor.isSelected())
                squareTraversal=new ColMajor(size);
            else if (radioDiagonal.isSelected())
                squareTraversal=new Diagonal(size);
            else if (radioAntiDiagonal.isSelected())
                squareTraversal=new AntiDiagonal(size);
            else if (radioClockwise.isSelected())
                squareTraversal=new Clockwise(size);
            else if (radioAntiClockwise.isSelected())
                squareTraversal=new AntiClockwise(size);

            displayCurrent();
        } else if (e.getSource() == buttonNext) {
            //下一步
            SquarePoint nextPoint = squareTraversal.getNext();
            squareTraversal.step(nextPoint);
            displayCurrent();
        }
    }

    private void displayCurrent() { // 只对同一个内部变量squareTraversal显示
        // 在这里添加代码：
        if (squareTraversal != null) {
            // 更新面板内容
            panelShow.removeAll();
            fieldType.setText(TYPES[whichType]);
            fieldSize.setText(boxSize.getSelectedItem().toString());

            // 设置显示面板的布局为网格布局，大小与方阵相同
            panelShow.setLayout(new GridLayout(squareTraversal.size, squareTraversal.size));

            // 获取当前矩阵状态
            int[][] currMatrix = squareTraversal.getCurrMatrix();
            // 获得当前点
            SquarePoint lastPoint = squareTraversal.currentPath.get(squareTraversal.currentPath.size() - 1);

            // 创建并添加表示方阵的标签
            for (int i = 0; i < squareTraversal.size; i++) {
                for (int j = 0; j < squareTraversal.size; j++) {
                    JLabel cell = new JLabel();
                    cell.setOpaque(true);
                    cell.setBorder(BorderFactory.createLineBorder(Color.BLACK));

                    if (currMatrix[i][j] == 1) {
                        // 如果是当前点
                        if ( i == lastPoint.row && j == lastPoint.col) {
                            //当前点为蓝色
                            cell.setBackground(Color.BLUE);
                        } else {
                            // 已遍历的点为红色
                            cell.setBackground(Color.RED);
                        }
                    } else {
                        // 未遍历的点为绿色
                        cell.setBackground(Color.GREEN);
                    }
                    panelShow.add(cell);
                }
            }

            panelShow.revalidate();
            panelShow.repaint();
        }
    }

    public static void main(String[] args) {
        new Visualization();
    }
}

